package myCine.alloCine.view;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import myCine.alloCine.model.app.result.SearchResult;
import myCine.alloCine.model.exchange.search.subClass.Movie;
import myCine.alloCine.view.resources.Resources;

public class OldView {

	public OldView(final String language) {
		
		if(language.equals(Resources.LANGUAGE_FR)){
			Resources.loadFrenchResources();
		}
	}
	
	/*=============================
	 *    	 DISPLAY ALL 
	 *=============================*/
	
	/**
	 * Display :
	 * - movies details wich respect criteria
	 * - movies titles which doesn't respect criteria
	 * - movies titles wich are not found
	 */
	public String diplsayAll(SearchResult searchresult){
		
		String result = "";
		
    	if(!searchresult.getListOfMovies().isEmpty()){
    		result += displayMovies(searchresult.getListOfMovies());
    	}
    	
    	result += Resources.LINE_BREAK;

    	if(!searchresult.getListOfMoviesBadCriteria().isEmpty()){
    		result += displayMovies(searchresult.getListOfMoviesBadCriteria(), Resources.MOVIES_BAD_CRITERIA);
    	}
    	
    	result += Resources.LINE_BREAK;
    	
    	if(!searchresult.getListOfMoviesNotFound().isEmpty()){
    		result += displayMoviesTitles(searchresult.getListOfMoviesNotFound(), Resources.MOVIES_NOT_FOUND);
    	}
    	
    	return result;
		
	}
	
	/*=============================
	 *    DISPLAY DETAILS MOVIES
	 *=============================*/
	
	private String displayMovie(final Movie movie){
		
		String userRate = "-";
		if(movie.getStatistics().getUserRating() != null){
			Double userRateValue =  (Math.ceil(movie.getStatistics().getUserRating()*100))/100;
			DecimalFormat df = new DecimalFormat("#.0#");
			userRate = df.format(userRateValue);
		}
		
		String stringResult = 
				  Resources.TITLE + ": " + movie.getOriginalTitle() + Resources.LINE_BREAK
				+ Resources.YEAR + ": " + movie.getProductionYear() + Resources.LINE_BREAK
				+ Resources.RATE + ": " + userRate + "/5" + Resources.LINE_BREAK
				+ Resources.USER_RATING_COUNT + ": " + movie.getStatistics().getUserRatingCount()  + Resources.LINE_BREAK
				+ Resources.SYNOPSIS + ": " +  Resources.LINE_BREAK + movie.getSynopsis();
		
		return stringResult;
		
	}
	
	private String displayMovies(final List<Movie> listOfMovies){
		
		String stringResult = "";
		
		for (int i = 0; i < listOfMovies.size(); i++) {
			
			Movie movie = listOfMovies.get(i);
			
			if(i > 0){
				stringResult += Resources.LINE_BREAK;
			}
			
			stringResult += displayMovie(movie) + Resources.LINE_BREAK;
			
			if(i < listOfMovies.size()-1){
				stringResult += "-------------------------------------------------" + Resources.LINE_BREAK;
			}
		}
		
		return stringResult;
	}
	
	
	/*=============================
	 *    DISPLAY MOVIES TITLES
	 *=============================*/

	private String displayMovies(List<Movie> listOfMovies, final String title) {
		
		List<String> listOfMoviesTitles = new ArrayList<String>();
		for(Movie movie : listOfMovies){
			listOfMoviesTitles.add(movie.getOriginalTitle());
		}
		
		return displayMoviesTitles(listOfMoviesTitles, title);
		
	}
	
	private String displayMoviesTitles(List<String> listOfMoviesTitles, final String title) {
		
		String stringResult = "######################## " + title + " ########################";
		stringResult += Resources.LINE_BREAK;
		
		for (int i = 0; i < listOfMoviesTitles.size(); i++) {
			
			if(i > 0){
				stringResult += Resources.LINE_BREAK;
			}
			
			stringResult += listOfMoviesTitles.get(i);
		}
		
		return stringResult;
	}
	
}
